package bcel.cardcenter.bconnex.recon.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import bcel.cardcenter.bconnex.recon.dao.BconTxnDao;
import bcel.cardcenter.bconnex.recon.entity.BconTxn;

@Component("logPersistenceService")
public class LogPersistenceServiceImp implements LogPersistenceService {
	@Value("./input/TEMP")
	private String source;
	@Autowired
	private BconTxnDao bconTxnDao;
	@Autowired
	private FileReaderService fileReaderService;
	public void setBconTxnDao(BconTxnDao bconTxnDao){
		this.bconTxnDao = bconTxnDao;
	}
	public void setFileReaderService(FileReaderService fileReaderService){
		this.fileReaderService = fileReaderService;
	}
	public void setSource(String source){
		this.source = source;
	}
	@Override
	public void logFileReader() throws Exception {
		File dir = new File(source);
		File[] fileSources = dir.listFiles();
		
		for(int i=0;i<fileSources.length;i++){
			String name = fileSources[i].getName();
			String sdate = name.substring(11, name.length());
			java.sql.Date date = str2Date(sdate);
			fileReaderService.verifyLogFileReader(fileSources[i],date);
			List<BconTxn> list = fileReaderService.getBconTxns();
			bconTxnDao.saveAll(list);
		}
	}
	private java.sql.Date str2Date(String sdate) throws Exception{
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
		java.util.Date udate = format.parse(sdate);
		return new java.sql.Date(udate.getTime());
	}
}
